Advanced Dice Roller



Documentation: Short version

ADR is a Messenger Plus! Live script for performing virtual dice rolls following d20 "formulae" (like 1d6, 2d10+4 or even more complex) from within the chat window, either submitting the results to the chat or showing them secretly via "toast" popup.
Usage in a nutshell:
"/roll <formula>" for rolling, evaluation and printing results in chat window
"/sroll <formula>" for the same except for showing results privately in a popup.

Documentation: Full [aka longwinded] version

Rationale behind script creation and feature highlight

ADR is a script for Messenger Plus! Live, which was inspired by ThrowDice script by Kai Kadgien aka Zerosan which allowed performing pseudorandom rolls of dice from within the IM.
However, tDice has, in my view, some limitations/inconveniences that could render it clumsy if one wished to roll dice extensively in, let's say, online D&D session. First, it supports only a fixed type of dice rolls - a set number of identical dice. Second, user input was done via a window separate from chat window, in a dialog-based rather than d20-formulae manner.

My attempt is to create a script that allows:
* Invoking roll directly from chat window, without need to switch to a separate dialog
* Performing complex dice rolls, expressed by (hopefully familiar) d20 system syntax
* Allowing either a public announce of roll results in current chat window or showing them "secretly" to the person rolling.

In my view, this should make actual use of this script in an online p&p RPG game easier. I've never actually played p&p RPGs, though, so experienced users' feedback is warmly welcome.

Usage instructions

ADR is invoked directly from chat window by /roll or /sroll commands.

Syntax ([] denotes optional parameters):
/roll <formula1>[<separator><formula2><separator>...]
/sroll <formula1>[<separator><formula2><separator>...]


<separator> is either ';' or ','
<formulaN> is a mathematical expression using 4 basic math operations, brackets, whole numbers and die rolls in d20 syntax, e.g. 3d6 or 1d% (see the language specification chapter for details)

The request is checked for well-fomedness. Formula is parsed, and rolls are substituted with thier die-per-die results. The resulting string is called "intermediate result". Then the mathematical expression is evaluated, and final result is presented in the form "<formula> -> <intermediate result> -> <final evaluation>".
Examples: "2d6 + 2 -> [3+5]+2 -> 10", "2*(1d%+3d6) -> 2*([36]+[5+5+1]) -> 94"
Depending on the command invoked, the result is then shown:
/roll -> in form of a "/me rolls <result>" message in current chat window;
/sroll -> in form of a "<result>" Messenger Plus! popup window, so-called "toast".

WARNING: The "/roll" command uses "/me" command and colour codes handled by Plus!. If the message is read by a non-Plus! user, it would look ugly. This may become optional in future versions.

Formula language specification

Informally: To construct a valid dice roll request, you take whole numbers and d20-form dice rolls and combine them using 4 basic math operations, +, -, *, /, and brackets. By d20-form dice roll I mean "<number of dice>d<die sides>". Examples: 1d6 for one 6-sided die, 4d12 for four 12-sided dice (results are summed up). A special case, d% can indicate 100-sided die.
Examples: 1d6, 2d4, 1d20+2, 3*(5d10-25)-3, ((1d2*3d6-1)-2d4)*(7d8), or whatever your needs or fantasy can construct.

Formally: Language description.
Spaces are icnored (cut out).
Any natural number (0,1,2,3 etc.) is accepted as "number". If something is a "number", it is also an "expression".
If X is a non-zero "number", and Y is a non-zero "number" or '%', then XdY is an "expression".
If X,Y are "expressions", then X+Y, X-Y, X*Y, X/Y and (X) are "expressions".
Unary + and - do not fit context-free grammar, but can be used as first character of a "subformula", i.e. first character of the formula or fist character after an opening bracket. [Examples: +1+1d6, 2*(-3d4+6)]
If the whole formula can be "typified" as "expression", it is considered well-formed.

WARNING: The script uses JScript's eval() method to calculate the finar result; if a division by zero occurs, it can't be caught, and the script fails. User gets feedback about unrecognized command. It can be countered in future versions by manually crafting evaluation engine.

Version history

1.1 - Bugfix: erroneously rejected formulas that contained "0d" as substring, while ex. "10d6" should be possible.

1.0 - Initial release - /roll, /sroll command, initial language specification, no preferences yet.

Todo list & ideas

- Write evaluation code form scratch to address possible division by zero exceptions.
- Add preferences as to result verbosity (enable/disable "intermediate result" display) and option to disable Plus! color-coding and '/me'.

Contact information & final words

This is my first script for Messenger Plus Live! and first program in JScript. I might have made some errors or just did something in a non-ratioanl way [thinking in Perl and then translating into whatever I could get out of MSDN site]. I've commented my code extensively, so anyone wishing to give programming opinions & feedback, or modify the program, is free to do so. If you modify and redistibute it, however, please give me some credit (at least in the source code).

I wrote this script in hope that someone would actually try to use it; if anyone is reading this and uses the script, please drop me a line. Critique welcome.

Author: Alexander Kashev (aka Xan, aka aleXANder)
Mailto: kav2k@mail.ru
MSN contact (don't use for e-mail!): mexmat.student@hotmail.com